package sites.manager.admin.role;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import sites.manager.common.kit.Ret;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.model.Account;
import sites.manager.common.model.Role;

import java.util.Date;

/**
 * 用户管理
 */
public class RoleService {

    private Role dao = new Role().dao();

    public Ret getRoles(String name, int page, int limit){
        Ret ret = Ret.ok();

        Kv kv = Kv.by("name", "%"+name+"%").set("siteId", SiteInfo.siteId);
        Page<Role> accountPage = dao.template("role.getRoles", kv).paginate(page, limit);

        //组装结果
        ret.setCode("0");
        ret.setData(accountPage.getList());
        ret.setCount(accountPage.getTotalRow());

        return ret;
    }

    public Ret updateStruts(int id, boolean flag){
        Ret ret = Ret.create();
        Role role = dao.findById(id);
        if(flag){
            role.setStatus(SiteInfo.statusNormal);
        } else {
            role.setStatus(SiteInfo.statusDel);
        }
        role.setUpdateTime(new Date());
        boolean t = role.update();
        if(t){
            ret.setOk();
            ret.setMsg("更新成功");
        }
        return ret;
    }

    public Ret save(Role role){
        Ret ret = Ret.ok();

        Role temp = getByName(role.getName());
        if(temp != null){
            ret.setFail().setMsg("名称已被占用，请换用其他名字");
            return ret;
        }

        role.setCreateTime(new Date());
        role.setStatus(SiteInfo.statusNormal);
        role.setSiteId(SiteInfo.siteId);
        boolean flag = role.save();

        if(!flag) {
            ret.setFail();
            ret.setMsg("保存失败");
        }

        return ret;
    }

    public Ret update(Role role){
        Ret ret = Ret.create();
        Role temp = getById(role.getId());
        temp.setName(role.getName());
        temp.setUpdateTime(new Date());
        boolean flag = temp.update();
        if(flag){
            ret.setOk();
            ret.set(SiteInfo.msgKey, "更新成功");
        } else {
            ret.setFail();
            ret.set(SiteInfo.msgKey, "更新失败，请联系管理员！");
        }
        return ret;
    }

    public Role getByName(String name){
        String sql = dao.getSql("role.getByName");
        return dao.findFirst(sql, SiteInfo.siteId, name);
    }

    public Role getById(int id){
        return dao.findById(id);
    }

    public Ret batchRemove(String ids){
        Ret ret = Ret.create();

        String[] idArray = ids.split(",");
        int num = 0;
        for (int i=0,j=idArray.length; i<j; i++){
            Role temp = getById(Integer.parseInt(idArray[i]));
            temp.setStatus(SiteInfo.statusDel);
            boolean flag = temp.update();
            if(flag){
                num++;
            }
        }

        if(num > 0){
            ret.setOk();
            ret.setMsg("成功禁用【"+num+"】个角色");
        } else {
            ret.setFail().setMsg("禁用失败，请联系管理员！");
        }

        return ret;
    }
}
